#
# CDDL HEADER START
#
# The contents of this file are subject to the terms of the
# Common Development and Distribution License (the "License").
# You may not use this file except in compliance with the License.
#
# You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
# or http://www.opensolaris.org/os/licensing.
# See the License for the specific language governing permissions
# and limitations under the License.
#
# When distributing Covered Code, include this CDDL HEADER in each
# file and include the License file at usr/src/OPENSOLARIS.LICENSE.
# If applicable, add the following below this CDDL HEADER, with the
# fields enclosed by brackets "[]" replaced with your own identifying
# information: Portions Copyright [yyyy] [name of copyright owner]
#
# CDDL HEADER END
#

#
# Copyright (c) 2010, Oracle and/or its affiliates. All rights reserved.
# Copyright 2015, OmniTI Computer Consulting, Inc. All rights reserved.
# Copyright 2015 Igor Kozhukhov <ikozhukhov@gmail.com>
# Copyright 2016 RackTop Systems.
# Copyright 2020 OmniOS Community Edition (OmniOSce) Association.
# Copyright 2020 Peter Tribble
#

include $(SRC)/Makefile.master
include $(SRC)/Makefile.buildnum

PKGS_TYPE= deb

# this variable can be used from illumos.sh for disable APT upload
NOAPT=

#
# To suppress package dependency generation on any system, regardless
# of how it was installed, set SUPPRESSPKGDEP=true in the build
# environment.
#
SUPPRESSPKGDEP= false

#
# Comment this line out or set "PKGDEBUG=" in your build environment
# to get more verbose output from the make processes in usr/src/pkg
#
PKGDEBUG= @

#
# Cross platform packaging notes
#
# By default, we package the proto area from the same architecture as
# the packaging build.  In other words, if you're running nightly or
# bldenv on an x86 platform, it will take objects from the x86 proto
# area and use them to create x86 repositories.
#
# If you want to create repositories for an architecture that's
# different from $(uname -p), you do so by setting PKGMACH in your
# build environment.
#
# For this to work correctly, the following must all happen:
#
#   1. You need the desired proto area, which you can get either by
#      doing a gatekeeper-style build with the -U option to
#      nightly(1), or by using rsync.  If you don't do this, you will
#      get packaging failures building all packages, because pkgsend
#      is unable to find the required binaries.
#   2. You need the desired tools proto area, which you can get in the
#      same ways as the normal proto area.  If you don't do this, you
#      will get packaging failures building onbld, because pkgsend is
#      unable to find the tools binaries.
#   3. The remainder of this Makefile should never refer directly to
#      $(MACH).  Instead, $(PKGMACH) should be used whenever an
#      architecture-specific path or token is needed.  If this is done
#      incorrectly, then packaging will fail, and you will see the
#      value of $(uname -p) instead of the value of $(PKGMACH) in the
#      commands that fail.
#   4. Each time a rule in this Makefile invokes $(MAKE), it should
#      pass PKGMACH=$(PKGMACH) explicitly on the command line.  If
#      this is done incorrectly, then packaging will fail, and you
#      will see the value of $(uname -p) instead of the value of
#      $(PKGMACH) in the commands that fail.
#
# Refer also to the convenience targets defined later in this
# Makefile.
#
PKGMACH=	$(MACH)

#
# ROOT, TOOLS_PROTO, and PKGARCHIVE should be set by nightly or
# bldenv.  These macros translate them into terms of $PKGMACH, instead
# of $ARCH.
#
PKGROOT.cmd=	$(ECHO) $(ROOT) | $(SED) -e s:/root_$(MACH):/root_$(PKGMACH):
PKGROOT=	$(PKGROOT.cmd:sh)
TOOLSROOT.cmd=	$(ECHO) $(TOOLS_PROTO) | $(SED) -e s:/root_$(MACH):/root_$(PKGMACH):
TOOLSROOT=	$(TOOLSROOT.cmd:sh)
PKGDEST.cmd=	$(ECHO) $(PKGARCHIVE) | $(SED) -e s:/$(MACH)/:/$(PKGMACH)/:
PKGDEST=	$(PKGDEST.cmd:sh)
PKGDESTAPT.cmd=	$(ECHO) $(PKGARCHIVEAPT) | $(SED) -e s:/$(MACH)/:/$(PKGMACH)/:
PKGDESTAPT=	$(PKGDESTAPT.cmd:sh)

EXCEPTIONS= packaging

PKGMOGRIFY= pkgmogrify

VALIDATEPKG.sparc= $(SRC)/tools/scripts/validate_pkg
VALIDATEPKG.i386= $(SRC)/tools/scripts/validate_pkg
VALIDATEPKG= $(VALIDATEPKG.$(MACH))

#
# Always build the redistributable repository, but only build the
# nonredistributable bits if we have access to closed source.
#
# Some objects that result from the closed build are still
# redistributable, and should be packaged as part of an open-only
# build.  Access to those objects is provided via the closed-bins
# tarball.  See usr/src/tools/scripts/bindrop.sh for details.
#
REPOS= redist

#
# The packages directory will contain the processed manifests as
# direct build targets and subdirectories for package metadata extracted
# incidentally during manifest processing.
#
# Nothing underneath $(PDIR) should ever be managed by SCM.
#
#PDIR= packages.$(PKGMACH)
PDIRDEB= $(CODEMGR_WS)/packages/$(PKGMACH)/debs

#
# The tools proto must be specified for dependency generation.
# Publication from the tools proto area is managed in the
# publication rule.
#
$(PDIR)/developer-build-onbld.dep:= PKGROOT= $(TOOLSROOT)

PKGPUBLISHER= $(PKGPUBLISHER_REDIST)

#
# To get these defaults, manifests should simply refer to $(PKGVERS).
#
PKGVERS_COMPONENT= 0.$(RELEASE)
PKGVERS_BUILTON= $(RELEASE)
PKGVERS_BRANCH= 0.$(ONNV_BUILDNUM)
PKGVERS= $(PKGVERS_COMPONENT),$(PKGVERS_BUILTON)-$(PKGVERS_BRANCH)
DEB_VERSION= 1.0.$(ONNV_BUILDNUM)-2
DEB_MAINTAINER= Maintainer <maintainer@illumos.org>
DPKG_BUILD_FLAGS= -d -b -uc -us

#
# The ARCH32 and ARCH64 macros are used in the manifests to express
# architecture-specific subdirectories in the installation paths
# for isaexec'd commands.
#
# We can't simply use $(MACH32) and $(MACH64) here, because they're
# only defined for the build architecture.  To do cross-platform
# packaging, we need both values.
#
i386_ARCH32= i86
sparc_ARCH32= sparcv7
i386_ARCH64= amd64
sparc_ARCH64= sparcv9

#
# macros and transforms needed by pkgmogrify
#
# If you append to this list using target-specific assignments (:=),
# be very careful that the targets are of the form $(PDIR)/pkgname.  If
# you use a higher level target, or a package list, you'll trigger a
# complete reprocessing of all manifests because they'll fail command
# dependency checking.
#
PM_TRANSFORMS= common_actions publish restart_fmri facets defaults \
	extract_metadata
PM_TRANSFORMS += devel
PM_FINAL_TRANSFORMS= strip_dependinfo
PM_INC= transforms manifests

JAVA_8_ONLY=
JAVA_11_ONLY=
$(BLD_JAVA_11)JAVA_8_ONLY=$(POUND_SIGN)
$(JAVA_8_ONLY)JAVA_11_ONLY=$(POUND_SIGN)

PKGMOG_DEFINES= \
	i386_ONLY=$(POUND_SIGN) \
	sparc_ONLY=$(POUND_SIGN) \
	$(PKGMACH)_ONLY= \
	ARCH=$(PKGMACH) \
	ARCH32=$($(PKGMACH)_ARCH32) \
	ARCH64=$($(PKGMACH)_ARCH64) \
	MACH32=$(MACH32) \
	MACH64=$(MACH64) \
	PKGVERS_COMPONENT=$(PKGVERS_COMPONENT) \
	PKGVERS_BUILTON=$(PKGVERS_BUILTON) \
	PKGVERS_BRANCH=$(PKGVERS_BRANCH) \
	PKGVERS=$(PKGVERS) \
	DEB_VERSION=$(DEB_VERSION) \
	__GNUC=$(__GNUC) \
	BUILD32=$(BUILD32) \
	BUILD64=$(BUILD64) \
	PYTHON3_VERSION=$(PYTHON3_VERSION) \
	PYTHON3_PKGVERS=$(PYTHON3_PKGVERS) \
	python3_ONLY=$(BUILDPY3) \
	python3tools_ONLY=$(BUILDPY3TOOLS) \
	JAVA_11_ONLY=$(JAVA_11_ONLY) \
	JAVA_8_ONLY=$(JAVA_8_ONLY)

PKGDEP_TOKENS_i386= \
	'PLATFORM=i86hvm' \
	'PLATFORM=i86pc' \
	'PLATFORM=i86xpv' \
	'ISALIST=amd64' \
	'ISALIST=i386'

PKGDEP_TOKENS_sparc= \
	'PLATFORM=sun4u' \
	'PLATFORM=sun4v' \
	'ISALIST=sparcv9' \
	'ISALIST=sparc'

PKGDEP_TOKENS= $(PKGDEP_TOKENS_$(PKGMACH))

#
# The package lists are generated with $(PKGDEP_TYPE) as their
# dependency types, so that they can be included by either an
# incorporation or a group package.
#
$(PDIR)/osnet-redist.mog := PKGDEP_TYPE= require
$(PDIR)/osnet-incorporation.mog:= PKGDEP_TYPE= incorporate

PKGDEP_INCORP= \
	depend fmri=consolidation/osnet/osnet-incorporation type=require

#
# All packaging build products should go into $(PDIR), so they don't
# need to be included separately in CLOBBERFILES.
#
CLOBBERFILES= $(PDIR) proto_list_$(PKGMACH) install-$(PKGMACH).out \
	license-list

#
# By default, PKGS will list all manifests.  To build and/or publish a
# subset of packages, override this on the command line or in the
# build environment and then reference (implicitly or explicitly) the all
# or install targets.
#
MANIFESTS :sh= (cd manifests; ls -1 *.p5m)

MANIFESTS += $(EXT_MANIFESTS)

PKGS= $(MANIFESTS:%.p5m=%)
#DEP_PKGS= $(PKGS:%=$(PDIR)/%.dep)
#PROC_PKGS= $(PKGS:%=$(PDIR)/%.mog)
#FIN_PKGS= $(PKGS:%=$(PDIR)/%.fin)

#DEB_PKGS= $(PKGS:%=$(PDIRDEB)/%.deb.stamp)
DEB_PKGS2= $(PDIRDEB)/all.deb.stamp
DEB_CHANGELOG= $(PDIRDEB)/deb.changelog.stamp
DEB_RULES= $(PDIRDEB)/deb.rules.stamp
DEB_PKGS_CHANGES.cmd= ($(ECHO) $(PDIRDEB)/*.changes)
DEB_PKGS_CHANGES= $(DEB_PKGS_CHANGES.cmd:sh)
DEB_PKGS_APT= $(DEB_PKGS_CHANGES:%_$(DEB_VERSION)_solaris-$(MACH).changes=%.apt)

CLOBBERFILES += $(EXT_MANIFESTS:%=manifests/%)
CLEANFILES += $(EXT_MANIFESTS:%=manifests/%) $(DEB_CHANGELOG) $(DEB_PKGS2) $(DEB_RULES)

#
# Track the synthetic manifests separately so we can properly express
# build rules and dependencies.  The synthetic and real packages use
# different sets of transforms and macros for pkgmogrify.
#
SYNTH_PKGS= osnet-incorporation osnet-redist
DEP_SYNTH_PKGS= $(SYNTH_PKGS:%=$(PDIR)/%.dep)
PROC_SYNTH_PKGS= $(SYNTH_PKGS:%=$(PDIR)/%.mog)

#
# Root of pkg image to use for dependency resolution
# Normally / on the machine used to build the binaries
#
PKGDEP_RESOLVE_IMAGE = /

#
# For each package, we determine the target repository based on
# manifest-embedded metadata.  Because we make that determination on
# the fly, the publication target cannot be expressed as a
# subdirectory inside the unknown-by-the-makefile target repository.
#
# In order to limit the target set to real files in known locations,
# we use a ".pub" file in $(PDIR) for each processed manifest, regardless
# of content or target repository.
#
PUB_PKGS= $(SYNTH_PKGS:%=$(PDIR)/%.pub) $(PKGS:%=$(PDIR)/%.pub)

#
# Any given repository- and status-specific package list may be empty,
# but we can only determine that dynamically, so we always generate all
# lists for each repository we're building.
#
# The meanings of each package status are as follows:
#
#	PKGSTAT		meaning
#	----------	----------------------------------------------------
#	noincorp	Do not include in incorporation or group package
#	obsolete	Include in incorporation, but not group package
#	renamed		Include in incorporation, but not group package
#	current		Include in incorporation and group package
#
# Since the semantics of the "noincorp" package status dictate that
# such packages are not included in the incorporation or group packages,
# there is no need to build noincorp package lists.
#
PKGLISTS= \
	$(REPOS:%=$(PDIR)/packages.%.current) \
	$(REPOS:%=$(PDIR)/packages.%.renamed) \
	$(REPOS:%=$(PDIR)/packages.%.obsolete)

$(ENABLE_EXT)include $(EXT_SRC)/illumos/pkg/Makefile

.KEEP_STATE:

.NO_PARALLEL: $(DEB_PKGS2) debapt $(DEB_PKGS_APT) debaptconf debaptindex

#.PARALLEL: $(PKGS) $(PROC_PKGS) $(DEB_PKGS) $(DEP_PKGS) \
#	$(PROC_SYNTH_PKGS) $(DEP_SYNTH_PKGS) $(PUB_PKGS)
.PARALLEL:

#.PARALLEL: $(PKGS) $(PROC_PKGS) $(DEP_PKGS) \
#	$(PROC_SYNTH_PKGS) $(DEP_SYNTH_PKGS) $(FIN_PKGS) $(PUB_PKGS)

#
# For a single manifest, the dependency chain looks like this:
#
#	raw manifest (mypkg.p5m)
#		|
#		| use pkgmogrify to process raw manifest
#		|
#	processed manifest (mypkg.mog)
#		|
#	   *    | use pkgdepend generate to generate dependencies
#		|
#	manifest with TBD dependencies (mypkg.dep)
#		|
#	   %    | use pkgdepend resolve to resolve dependencies
#		|
#	manifest with dependencies resolved (mypkg.res)
#		|
#		| use pkgmogrify to apply final cleanups
#		|
#	cleaned up manifest (mypkg.fin)
#		|
#		| use pkgsend to publish the package
#		|
#	placeholder to indicate successful publication (mypkg.pub)
#
# * This may be suppressed via SUPPRESSPKGDEP.  The resulting
#   packages will install correctly, but care must be taken to
#   install all dependencies, because pkg will not have the input
#   it needs to determine this automatically.
#
# % This is included in this diagram to make the picture complete, but
#   this is a point of synchronization in the build process.
#   Dependency resolution is actually done once on the entire set of
#   manifests, not on a per-package basis.
#
# The full dependency chain for generating everything that needs to be
# published, without actually publishing it, looks like this:
#
#	processed synthetic packages
#		|		|
#       package lists	    synthetic package manifests
#		|
#	processed real packages
#	    |		|
#	package dir	real package manifests
#
# Here, each item is a set of real or synthetic packages.  For this
# portion of the build, no reference is made to the proto area.  It is
# therefore suitable for the "all" target, as opposed to "install."
#
# Since each of these steps is expressed explicitly, "all" need only
# depend on the head of the chain.
#
# From the end of manifest processing, the publication dependency
# chain looks like this:
#
#		repository metadata (catalogs and search indices)
#			|
#			| pkgrepo refresh
#			|
#		published packages
#		 |		|
#		 |		| pkgsend publish
#		 |		|
#	  repositories	    final manifests
#		 |			|
# pkgsend	 |			| pkgmogrify final
# create-repository			|
#		 |		resolved dependencies
#	 repo directories		|
#					| pkgdepend resolve
#					|
#				    generated dependencies
#					|
#					| pkgdepend
#					|
#                                    processed manifests

ALL_TARGETS= $(PROC_SYNTH_PKGS) proto_list_$(PKGMACH)

#all: $(ALL_TARGETS)
all: deb

#
# This will build the directory to contain the processed manifests
# and the metadata symlinks.
#
$(PDIR):
	@$(ECHO) "Creating $(@)"
	$(PKGDEBUG)$(INS.dir)

$(PDIRDEB):
	@$(ECHO) "Creating $(@)"
	$(PKGDEBUG)$(INS.dir)

$(PDIRDEB)/debian: $(PDIRDEB) $(SRC)/pkg/debs/debian
	$(RM) $@
	$(SYMLINK) $(SRC)/pkg/debs/debian $@

$(PKGDESTAPT):
	@$(ECHO) "Creating $(@)"
	$(PKGDEBUG)$(INS.dir)

install: install_$(PKGS_TYPE)

install_ips: $(ALL_TARGETS) repository-metadata

#install_deb: $(EXT_MANIFESTS) .WAIT deb .WAIT $(NOAPT)debapt
install_deb: deb .WAIT $(NOAPT)debapt

deb:	$(PDIRDEB) .WAIT $(DEB_PKGS2)

debapt: debaptindex

#
#
#
$(DEB_CHANGELOG): $(PDIRDEB)/debian
	-$(RM) debs/debian/changelog
	cd debs ; dch --create --newversion $(DEB_VERSION) -M --package dg2 --urgency low --distribution unstable "Build only"
	$(TOUCH) $@

$(DEB_RULES): $(PDIRDEB)/debian
	-$(RM) debs/debian/rules
	$(SED) 's#%%PROTOBLD%%#$(CODEMGR_WS)#g' < debs/debian/rules.tmpl > debs/debian/rules
	chmod +x debs/debian/rules
	$(TOUCH) $@

#
# Initialize the empty on-disk repositories
#
$(PKGDESTAPT)/conf/distributions: $(PKGDESTAPT)
	@$(ECHO) "Configuring APT repo"
	$(PKGDEBUG)mkdir -p $(PKGDESTAPT)/conf
	$(PKGDEBUG)$(CP) $(DEVAPTCFG) $(PKGDESTAPT)/conf/distributions

#
# rule to process real manifests
#
# To allow redistributability and package status to change, we must
# remove not only the actual build target (the processed manifest), but
# also the incidental ones (the metadata-derived symlinks).
#
# If pkgmogrify exits cleanly but fails to create the specified output
# file, it means that it encountered an abort directive.  That means
# that this package should not be published for this particular build
# environment.  Since we can't prune such packages from $(PKGS)
# retroactively, we need to create an empty target file to keep make
# from trying to rebuild it every time.  For these empty targets, we
# do not create metadata symlinks.
#
# Automatic dependency resolution to files is also done at this phase of
# processing.  The skipped packages are skipped due to existing bugs
# in pkgdepend.
#
# The incorporation dependency is tricky: it needs to go into all
# current and renamed manifests (ie all incorporated packages), but we
# don't know which those are until after we run pkgmogrify.  So
# instead of expressing it as a transform, we tack it on ex post facto.
#
# Implementation notes:
#
# - The first $(RM) must not match other manifests, or we'll run into
#   race conditions with parallel manifest processing.
#
# - The make macros [ie $(MACRO)] are evaluated when the makefile is
#   read in, and will result in a fixed, macro-expanded rule for each
#   target enumerated in $(PROC_PKGS).
#
# - The shell variables (ie $$VAR) are assigned on the fly, as the rule
#   is executed.  The results may only be referenced in the shell in
#   which they are assigned, so from the perspective of make, all code
#   that needs these variables needs to be part of the same line of
#   code.  Hence the use of command separators and line continuation
#   characters.
#
# - The extract_metadata transforms are designed to spit out shell
#   variable assignments to stdout.  Those are published to the
#   .vars temporary files, and then used as input to the eval
#   statement.  This is done in stages specifically so that pkgmogrify
#   can signal failure if the manifest has a syntactic or other error.
#   The eval statement should begin with the default values, and the
#   output from pkgmogrify (if any) should be in the form of a
#   variable assignment to override those defaults.
#
# - When this rule completes execution, it must leave an updated
#   target file ($@) in place, or make will reprocess the package
#   every time it encounters it as a dependency.  Hence the "touch"
#   statement to ensure that the target is created, even when
#   pkgmogrify encounters an abort in the publish transforms.
#

.SUFFIXES: .deb

$(SRC)/pkg/%: $(SRC)/pkg/%.$(MACH)
	$(CP) $< $@

# nothing to be done

# new process
$(PDIRDEB)/all.deb.stamp: $(PDIRDEB) $(PDIRDEB)/debian $(DEB_CHANGELOG) $(DEB_RULES)
	$(PKGDEBUG)( \
	    cd "$(PDIRDEB)" ; \
	    $(SUDO) debian/rules clean ; \
	    $(SUDO) -E dpkg-buildpackage $(DPKG_BUILD_FLAGS) )
	-$(SUDO) $(MV) $(PDIRDEB)/../*.deb $(PDIRDEB)/
	-$(SUDO) $(MV) $(PDIRDEB)/../*.changes $(PDIRDEB)/
	-$(SUDO) $(MV) $(PDIRDEB)/../*.buildinfo $(PDIRDEB)/
	$(PKGDEBUG)$(TOUCH) $@
#end new process

debaptindex: debapt2
	@$(ECHO) "Indexing APT repository"
	$(PKGDEBUG)/usr/bin/reprepro -b $(PKGDESTAPT) export
	@$(ECHO) "Local APT repo '$(PKGDESTAPT)' is ready to use"

debapt2: $(PKGDESTAPT)/conf/distributions
	$(PKGDEBUG)for p in $(PDIRDEB)/*.changes; do \
	    $(ECHO) "Sending $$p to the local APT ..." ; \
	    $(SUDO) $(RM) -r $(PKGDESTAPT)/db $(PKGDESTAPT)/dist $(PKGDESTAPT)/pool ; \
	    /usr/bin/reprepro -b $(PKGDESTAPT) --export=never include unstable $$p ; \
	    $(TOUCH) `echo $$p | cut -f1 -d'_'`.apt ; \
	done

$(PDIRDEB)/%.apt: $(PDIRDEB)/%_$(DEB_VERSION)_solaris-$(MACH).changes
	$(PKGDEBUG)$(ECHO) "Sending pkg '$(@F:%.apt=%)' to APT ..."; \
	    /usr/bin/reprepro -b $(PKGDESTAPT) --export=never remove unstable $(@F:%.apt=%) 2> /dev/null 1>&2; \
	    /usr/bin/reprepro -b $(PKGDESTAPT) --export=never include unstable $(<) 2> /dev/null 1>&2
	@$(TOUCH) $@


clean: clean_$(PKGS_TYPE)
	$(RM) -r $(SRC)/pkg/libc.postinst

clean_ips:

clean_deb: $(DEB_RULES) $(DEB_CHANGELOG)
	-$(SUDO) $(RM) -r $(PDIRDEB)
	-$(SUDO) $(RM) -r $(PDIR)/*.deb $(PDIR)/*.changes $(PDIR)/*.buildinfo
	if [ -f $(SRC)/pkg/debs_md5sum.out ]; then \
	    mv $(SRC)/pkg/debs_md5sum.out $(SRC)/pkg/debs_md5sum.out.pre.$(DEB_VERSION) ; \
	fi
	cd $(SRC)/pkg/debs ; $(SUDO) debian/rules clean
	-$(RM) debs/debian/changelog debs/debian/rules

clobber: clobber_$(PKGS_TYPE)

clobber_ips: clean_ips
	$(RM) -r $(CLOBBERFILES)

clobber_deb: clean_deb
	-$(SUDO) $(RM) -r $(PKGDESTAPT)
	-$(SUDO) $(RM) -r $(PDIR)

#check: protocmp pmodes pkglint
#check: protocmp pmodes
check pmodes stage-licenses protocmp:

#
# These are convenience targets for cross-platform packaging.  If you
# want to build any of "the normal" targets for a different
# architecture, simply use "arch/target" as your build target.
#
# Since the most common use case for this is "install," the architecture
# specific install targets have been further abbreviated to elide "/install."
#
i386/% sparc/%:
	$(MAKE) -e $(@F) PKGMACH=$(@D) SUPPRESSPKGDEP=$(SUPPRESSPKGDEP)

i386 sparc: $$(@)/install

FRC:
